<template>
  <el-dialog :title="tle" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="单品名称" prop="title">
        <el-input placeholder="" v-model="dataForm.title" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="出库数量" prop="stock" v-if="type==0">
        <el-input-number v-model="dataForm.stock" :precision="0" :min="1" :max="999999"></el-input-number>
      </el-form-item>
      <el-form-item label="入库数量" prop="stock" v-if="type==1">
        <el-input-number v-model="dataForm.stock" :precision="0" :min="1" :max="999999"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  treeDataTranslate
} from '@/utils'
import seviceApi from '@/api/serviceApi.js'
export default {
  data () {
    return {
      visible: false,
      tle: '',
      type: '', // 0 出库，1 入库
      dataForm: {
        id: '0',
        title: '',
        stock: null
      },
      dataRule: {
        stock: [
          { required: true, message: '出库数量不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id, type) {
      if (type == 0) {
        this.tle = '添加出库单'
        this.type = 0
      } else {
        this.tle = '添加入库单'
        this.type = 1
      }
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      seviceApi.getSinglegoodsInfo(id).then(res => {
        if (res && res.data.code == 200) {
          this.dataForm = res.data.data
        }
      })
    },
    // 提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.type == 0) {
            // able_stock 现有库存数量，stock 出库数量
            if (this.dataForm.able_stock < this.dataForm.stock) {
              this.$message({
                message: '出库数量大于现有库存数量',
                type: 'warning'
              })
            } else {
              seviceApi.saveSinglegoodsOutStock(this.dataForm).then(res => {
                if (res && res.data.code == 200) {
                  this.$message({
                    message: '出库添加成功',
                    type: 'success'
                  })
                  this.visible = false
                  this.$emit('updataList', 1)
                }
              })
            }
          } else {
            seviceApi.saveSinglegoodsInStock(this.dataForm).then(res => {
              if (res && res.data.code == 200) {
                this.$message({
                  message: '入库成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('updataList', 1)
              }
            })
          }
        }
      })
    }
  }
}
</script>

<style>
</style>
